package com.web.safe;

import com.blade.Blade;
import com.blade.mvc.RouteContext;

public class Application {
    public static void main(String[] args) {
        Blade.of().get("/login", ctx -> ctx.render("login.html"))
                .get("/logout", ctx -> {
                    ctx.session().remove("login_user");
                    ctx.redirect("/login");
                })
                .post("/login", ctx -> {
                    String username = ctx.query("username");
                    ctx.session().attribute("login_user",username);
                    ctx.text(username + "登录成功");
                })
                .get("/transfer", Application::transfer)
                .post("/transfer",Application::transfer)
                .start(Application.class, args);
    }

    private static void transfer(RouteContext ctx) {
        String username = ctx.session().attribute("login_user");
        if (username == null) {
            System.out.println("用户未登录");
            ctx.text("请登录后操作!");
            return;
        }

        String toUser = ctx.query("to_user");
        int money = ctx.queryInt("money");
        String msg = username + " 给 [" + toUser + "] 转账 " + money + "元成功";
        System.out.println(msg);
        ctx.text(msg);
    }
}
